﻿@using Microsoft.AspNetCore.Components.Rendering
@using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure
@namespace Microsoft.FluentUI.AspNetCore.Components
@typeparam TGridItem
@{
    InternalGridContext.Grid.AddColumn(this, InitialSortDirection, IsDefaultSortColumn);
}
@code
{
    private void RenderDefaultHeaderContent(RenderTreeBuilder __builder)
    {
        @if (HeaderCellItemTemplate is not null)
        {
            @HeaderCellItemTemplate(this)
        }
        else
        {
            string? tooltip = Tooltip ? Title : null;

            @if (ColumnOptions is not null && (Align == Align.Start || Align == Align.Center))
            {
                <FluentButton Appearance="Appearance.Stealth" class="col-options-button" @onclick="@(() => Grid.ShowColumnOptionsAsync(this))" aria-label="Filter this column">
                    @if (Filtered.GetValueOrDefault())
                    {
                        <FluentIcon Value="@(new CoreIcons.Regular.Size24.FilterDismiss())" />
                    }
                    else
                    {
                        <FluentIcon Value="@(new CoreIcons.Regular.Size24.Filter())" />
                    }
                </FluentButton>
            }

            if (Sortable.HasValue ? Sortable.Value : IsSortableByDefault())
            {
                <FluentKeyCode Only="new [] { KeyCode.Ctrl, KeyCode.Enter}" OnKeyDown="HandleKeyDown" class="keycapture">
                <span class="col-sort-container" @oncontextmenu="@(() => Grid.RemoveSortByColumnAsync(this))" @oncontextmenu:preventDefault>
                    <FluentButton Appearance="Appearance.Stealth" class="col-sort-button" @onclick="@(() => Grid.SortByColumnAsync(this))" aria-label="@tooltip" title="@tooltip">
                        <div class="col-title-text" title="@tooltip">@Title</div>

                        @if (Grid.SortByAscending.HasValue && ShowSortIcon)
                        {
                            if (Grid.SortByAscending == true)
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.ArrowSortUp())" Slot="@(Align == Align.End ? "start" : "end")" />
                            }
                            else
                            {
                                <FluentIcon Value="@(new CoreIcons.Regular.Size24.ArrowSortDown())" Slot="@(Align == Align.End ? "start" : "end")" />
                            }
                        }

                    </FluentButton>
                </span>
                </FluentKeyCode>
            }
            else
            {
                <div class="col-title">
                    <div class="col-title-text" title="@tooltip">@Title</div>
                </div>
            }

            @if (ColumnOptions is not null && Align == Align.End)
            {
                <FluentButton Appearance="Appearance.Stealth" class="col-options-button" @onclick="@(() => Grid.ShowColumnOptionsAsync(this))" aria-label="Filter this column">
                    @if (Filtered.GetValueOrDefault())
                    {
                        <FluentIcon Value="@(new CoreIcons.Regular.Size24.FilterDismiss())" />
                    }
                    else
                    {
                        <FluentIcon Value="@(new CoreIcons.Regular.Size24.Filter())" />
                    }
                </FluentButton>
            }
        }
    }

    internal void RenderPlaceholderContent(RenderTreeBuilder __builder, PlaceholderContext placeholderContext)
    {
        // Blank if no placeholder template was supplied, as it's enough to style with CSS by default
        if (PlaceholderTemplate is not null)
        {
            @PlaceholderTemplate(placeholderContext)
        }
    }
}
